#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include "snmprdbms.h"
#include "snmprdbms_config.h"
#include "snmprdbms_ds.h"
#include "rdbmsMIB_columns.h"
#include "snmprdbms_odbc.h"
#include "snmprdbms_data.h"

netsnmp_cache *data_get_cache() {
  return (netsnmp_cache *)snmprdbms_ds_get_void(DS_ID_CACHE);
}

void data_set_cache(netsnmp_cache *cache) {
  snmprdbms_ds_set_void(DS_ID_CACHE, cache);
}

rdbmsdata *data_get() {
  return (rdbmsdata *)snmprdbms_ds_get_void(DS_ID_DATA);
}

void data_set(rdbmsdata *data) {
  snmprdbms_ds_set_void(DS_ID_DATA, data);
}

void data_free(netsnmp_cache *cache, void *foo) {
  rdbmsdata *data = data_get();

  if(data == NULL)
    return;

  odbc_free_table(data->rdbmsDbInfoTable);
  odbc_free_table(data->rdbmsDbLimitedResourceTable);
  odbc_free_table(data->rdbmsDbParamTable);
  odbc_free_table(data->rdbmsDbTable);
  odbc_free_table(data->rdbmsRelTable);
  odbc_free_table(data->rdbmsSrvInfoTable);
  odbc_free_table(data->rdbmsSrvLimitedResourceTable);
  odbc_free_table(data->rdbmsSrvParamTable);
  odbc_free_table(data->rdbmsSrvTable);

  FREE_AND_NULL(data);

  data_set(NULL);

}

static unsigned long sdbm(unsigned char *str) {
	unsigned long hash = 0;
    int c;

    while (c = *str++)
    	hash = c + (hash << 6) + (hash << 16) - hash;

    return hash;
}

int data_load(netsnmp_cache *cache, void *foo) {
  debug("Reload cache\n");

  rdbmsdata *data = data_get();
  conf_instance_param *instance = config_ds_get_instance(-1); //get first instance
  odbcdatatable *tmp_table, *target_table = NULL;

  if (data == NULL) {
    data = calloc(1, sizeof(rdbmsdata)); //memory set zero
    data_set(data);
  }

  while (instance != NULL) {

	if (odbc_connect(instance->cstring) == SNMPRDBMS_ERROR)
		return SNMPRDBMS_ERROR;

	//rdbmsSrvTable
	tmp_table = odbc_execute(conf_ds_get_instance_type_param("rdbmsSrvTable", instance->instance_id));
	odbc_cut_table(tmp_table);
	data->rdbmsSrvTable = odbc_append_table(data->rdbmsSrvTable, tmp_table);

	//rdbmsSrvInfoTable
	tmp_table = odbc_execute(conf_ds_get_instance_type_param("rdbmsSrvInfoTable", instance->instance_id));
	odbc_cut_table(tmp_table);
	data->rdbmsSrvInfoTable = odbc_append_table(data->rdbmsSrvInfoTable, tmp_table);

	//rdbmsSrvLimitedResourceTable
	tmp_table = odbc_execute(conf_ds_get_instance_type_param("rdbmsSrvLimitedResourceTable", instance->instance_id));
	odbc_cut_table(tmp_table);
	data->rdbmsSrvLimitedResourceTable = odbc_append_table(data->rdbmsSrvLimitedResourceTable, tmp_table);

	//rdbmsSrvParamTable
	tmp_table = odbc_execute(conf_ds_get_instance_type_param("rdbmsSrvParamTable", instance->instance_id));
	odbc_cut_table(tmp_table);
	data->rdbmsSrvParamTable = odbc_append_table(data->rdbmsSrvParamTable, tmp_table);

	//rdbmsDbTable
	tmp_table = odbc_execute(conf_ds_get_instance_type_param("rdbmsDbTable", instance->instance_id));
	debug("HASH: %u\n", sdbm( odbc_get_named_field("rdbmsDbName", tmp_table) ) );
	data->rdbmsDbInfoTable = odbc_append_table(data->rdbmsDbInfoTable, tmp_table);

	/*
	//rdbmsDbLimitedResourceTable
	tmp_table = odbc_execute(conf_ds_get_instance_type_param("rdbmsDbLimitedResourceTable", instance->instance_id));
	data->rdbmsDbLimitedResourceTable = odbc_append_table(data->rdbmsDbLimitedResourceTable, tmp_table);

	//rdbmsDbParamTable
	tmp_table = odbc_execute(conf_ds_get_instance_type_param("rdbmsDbParamTable", instance->instance_id));
	data->rdbmsDbParamTable = odbc_append_table(data->rdbmsDbParamTable, tmp_table);

	//rdbmsDbTable
	tmp_table = odbc_execute(conf_ds_get_instance_type_param("rdbmsDbTable", instance->instance_id));
	data->rdbmsDbTable = odbc_append_table(data->rdbmsDbTable, tmp_table);

	//rdbmsRelTable
	tmp_table = odbc_execute(conf_ds_get_instance_type_param("rdbmsRelTable", instance->instance_id));
	data->rdbmsRelTable = odbc_append_table(data->rdbmsRelTable, tmp_table);
	*/

    instance = instance->next;
    odbc_disconnect();

  }

  return SNMPRDBMS_SUCCESS;
}
